package composite;

import java.util.ArrayList;
import java.util.List;

public class Menu extends MenuComponent {
    private String name;
    private List<MenuComponent> menuComponents = new ArrayList<>();

    public Menu(String name) {
        this.name = name;
    }

    public void add(MenuComponent menuComponent) {
        menuComponents.add(menuComponent);
    }

    public void remove(MenuComponent menuComponent) {
        menuComponents.remove(menuComponent);
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public void print(String space) {
        // 打印根菜单名称（这里的根并不特指 ROOT）
        System.out.println(space + this.name);
        // 遍历根下的子菜单名称
        for(MenuComponent menuComponent : menuComponents) {
            menuComponent.print(space + " ");
        }
    }
}
